//
//  SettingViewController.m
//  DrawPad
//
//  Created by mulin on 3/8/14.
//  Copyright (c) 2014 single.developer.dandan. All rights reserved.
//

#import "SettingViewController.h"

@interface SettingViewController ()
@property (weak, nonatomic) IBOutlet UILabel *brushValueDisplay;

@property (weak, nonatomic) IBOutlet UILabel *opacityValueDisplay;

@property (weak, nonatomic) IBOutlet UISlider *brushControl;
@property (weak, nonatomic) IBOutlet UISlider *opacityControl;
@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    self.brushControl.value = self.brush;
    [self slideChanged:self.brushControl];
    
    self.opacityControl.value = self.opacity;
    [self slideChanged:self.opacityControl];
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"brush"];
    [self removeObserver:self forKeyPath:@"opacity"];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"brush"])
    {
        self.brushValueDisplay.text = [NSString stringWithFormat:@"%.1f", self.brush];
    } else if ([keyPath isEqualToString:@"opacity"]) {
        self.opacityValueDisplay.text = [NSString stringWithFormat:@"%.1f", self.opacity];
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
     [self addObserver:self forKeyPath:@"brush" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
     [self addObserver:self forKeyPath:@"opacity" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonPressed:(id)sender {
    [self.delegate closeSettings:self];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)slideChanged:(id)sender {
    UISlider *slide = (UISlider *)sender;
    if (slide == self.brushControl) {
        self.brush = self.brushControl.value;
       
    } else if (slide == self.opacityControl) {
        self.opacity = self.opacityControl.value;
       
    }
}

@end
